<template>
  <!-- 通用商品详情页 -->
  <div>
    <swiper :imgList="imgList"></swiper>
    <p class="names">{{goods.goodsName}}</p>
    <ul class="price">
      <li>原价：{{goods.goodsPrice}}元</li>
      <li><span style="color: #ff8551">优享价：{{goods.vipPrice}}元</span><span class="xiaoliang">销量：{{goods.sales}}</span></li>
    </ul>
    <div class="divide"></div>
    <p class="title">详情介绍</p>
    <div style="margin-bottom: 30px">
      <p v-html="goods.goodsDesc"></p>
    </div>
    <ul class="bottom" v-if="entrySource=='integralshop'">
      <li><p>￥200</p>支付积分</li>
      <li> <wv-button type="default" :plain="true" @click="isVisible=true">兑换</wv-button></li>
    </ul>
    <div class="model" v-if="isVisible">
      <div class="bg"></div>
      <div class="modelContent">
        <img src="../../assets/close.png" alt="" width="15" @click="isVisible=false">
        兑换后将会扣除商品对应的积分， 是否确定兑换？
        <p class="btn">
          <button @click="goExchange(goods)">确认兑换</button>
          <button @click="isVisible=false">取消兑换</button>
        </p>
      </div>
      <div class="modelContent buzu" style="display:none;">
        <img src="../../assets/close.png" alt="" width="15" @click="isVisible=false">
        对不起，您的积分不足，无法兑换
        <p class="btn">
          <button @click="isVisible=false">确定</button>
        </p>
      </div>
    </div>
    <gouwuche v-if="entrySource!='integralshop'":cartParams="cartParams" :goods="goods" :isOld="isOld"></gouwuche>
  </div>
</template>

<script type="text/ecmascript-6">
  import swiper from '../offertemplate/imgswipertemplate'
  import gouwuche from '../offertemplate/gouwuche'
  import {getGoodDetail,exchangeGoods} from '../../../test/unit/http'
  import {wxApi} from '../../../test/unit/flyme_weixin'
  import global from '../../../test/unit/global'
  import {Toast} from 'we-vue'
  export default {
    name: 'goodsdetail',
    props: {},
    data() {
      return {
        imgList: [],
        goods: {},
        cartParams: {desc: '立即购买'},
        isVisible:false,
        entrySource: 'home',
        isOld:false
      };
    },
    methods: {
      goExchange(goods){
        let params={integral:goods.integralValue,goodsId:goods.goodsId,balanceType:2,type:3};
        exchangeGoods(params).then(res=>{
          console.log(res);
          if(res.code==100){
            this.$router.push({path: '/jifenjilu'});
          }
          else{
            Toast.fail(res.info);
          }
        })
      },
      getGoodsDetail(params){
        let that = this;
        getGoodDetail(params).then(res => {
          console.log(res);
          if (res.code == 100) {
            that.goods = res.object.goods;
            that.isOld = res.object.isOld;
            let goodsImg=that.goods.goodsImg;
            if(!this.$myUtils.isNull(that.goods.goodsImg)){
              that.imgList = goodsImg.split(",");
            }
          } else {
            Toast.fail(res.info)
          }
        })
      }
    },
    created() {
      let goodsId = this.$route.query.goodsId;
      this.entrySource = this.$route.query.entrySource;
      let params = {goodsId: goodsId};
      this.getGoodsDetail(params);
      let url = window.location.href;
      let shareUrl = this.$myUtils.initShareUrl(url);
      let that=this;
      wxApi.init(function () {
        wxApi.updateAppMessageShareData(that.goods.goodsName, that.goods.goodsName, shareUrl,  global.ImgBaseURL+'/'+that.goods.coverImg)
      });
    },
    components: {
      swiper,
      gouwuche
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" rel="stylesheet/less">
  .bottom{
    background-color: #eee;
    display: flex;
    padding: 10px;
    position: fixed;
    bottom: 0;
    width: 100%;
    li{
      flex: 1;

    }
    :first-child {
      font-size: 12px;
      color: #999;
      text-align: left;
      p{
        color: #fc8d4e;
        font-size: 18px;
      }
    }
    :last-child{
      button{
        background-image: linear-gradient(to left, #ff8551,#f9964e);
        color: #fff;
        height: 40px;
        line-height: 40px;
        border-radius: 25px;
        border:none;
        width: 160px;
        text-align: center;
      }
    }
  }
  .names {
    font-size: 18px;
    color: #111;
    font-weight: 700;
    padding: 10px;
    text-align: left;
  }

  .price {
    padding: 10px;
    text-align: left;
    li:first-child {
      font-size: 15px;
      color: #999;
    }
    li:last-child {
      font-size: 15px;
      color: #000;
      font-weight: 700;
      .xiaoliang {
        float: right;
        font-size: 12px;
        color: #999;
      }
    }
  }

  .title {
    font-size: 16px;
    color: #999;
    font-weight: 700;
    padding: 10px;
    text-align: left;
  }

  .divide {
    background-color: #eee;
    height: 10px;
  }

  .detail {
    font-size: 15px;
    color: #666;
    text-align: left;
    padding: 10px;

    p {
      text-indent: 30px;
      line-height: 25px;
    }
    ul {
      margin-top: 10px;
      li {
        line-height: 25px;
      }
    }
  }
  .bg {
    background-color: rgba(0, 0, 0, 0.3);
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 50px;
  }
  .model .modelContent {
    background-color: #fff;
    border-radius: 8px;
    position: fixed;
    left: 50%;
    top: 50%;
    margin-left: -43%;
    margin-top: -111px;
    z-index: 2;
    padding: 40px 10px 20px;
    width: 80%;
    text-align: left;
    img {
      position: absolute;
      right: 10px;
      top: 10px
    }
    .btn {
      button {
        height: 42px;
        line-height: 42px;
        color: #fff;
        outline: none;
        border: none;
        width: calc(50% - 20px);
        border-radius: 8px;
        margin-top: 25px;
      }
      :last-child {
        background-color: #ccc;

      }
      :first-child {
        background-color: #2f95f0;
        margin-right: 20px;

      }
    }

  }

  .model .buzu {
    text-align: center;
    .btn button {
      width: 100%;
    }
  }
</style>
